/**
 * 由 WangYu 创建
 * 日期 2017/9/25
 */
import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Image,
    TouchableOpacity, Dimensions, Alert, Linking
} from 'react-native';
import CommonNavigationBar from "../../view/CommonNavigationBar";
import ItemView from "../../view/ItemView";
import DashedLine from "../../view/DashedLine";
import Button from "../../view/Button";
import {connect} from "react-redux";
import {Actions} from "react-native-router-flux";
import NetUtils from "../../util/NetUtils";
import Toast from "teaset/components/Toast/Toast";
import Api from "../../util/Api";
import {logout} from "../../redux/action";
import Spinner from "../../view/Spinner";
let screenWidth = Dimensions.get('window').width;
class MorePage extends Component {

    constructor(props) {
        super(props);
        this.state = {
            progressVisible: false,
        }
    }

    render() {
        return (
            <View style={styles.container}>
                <CommonNavigationBar title="更多" showLeftIcon={false}/>
                <View style={{backgroundColor: '#dadada', paddingLeft: 8, paddingRight: 8}}>
                    <View style={styles.itemStyle}>
                        <ItemView icon={require('../../../images/more_xxzx.png')} title='消息中心'
                                  onPress={() => Actions.MessageListPage()}/>
                        <DashedLine width={screenWidth - 44}/>
                        <ItemView icon={require('../../../images/more_bzzx.png')} title='帮助中心'
                                  onPress={() => Actions.WebViewPage({url: Api.help})}/>
                        <DashedLine width={screenWidth - 44}/>
                        <ItemView icon={require('../../../images/more_yjfk.png')} title='意见反馈'
                                  onPress={() => this.props.isLogin || Api.isLogin ? Actions.FeedBackPage() : Actions.LoginPage()}/>
                        <DashedLine width={screenWidth - 44}/>
                        <ItemView icon={require('../../../images/more_gzwm.png')} title='关注我们'
                                  onPress={() => this.followUs()}/>
                        <DashedLine width={screenWidth - 44}/>
                        <ItemView icon={require('../../../images/more_gwgl.png')} title='给我鼓励'
                                  onPress={()=>this.openMarket()}/>
                        <DashedLine width={screenWidth - 44}/>
                        <ItemView icon={require('../../../images/more_gyyd.png')} title='关于微微贷'
                                  onPress={()=>Actions.AboutUsPage()}/>
                        <DashedLine width={screenWidth - 44}/>
                        <Button title={(this.props.isLogin && Api.isLogin) ? '退出登录' : '登录'}
                                onPress={() => this.props.isLogin || Api.isLogin ? this.logout() : Actions.LoginPage()}
                                style={{marginTop: 30, marginBottom: 30}}/>
                    </View>
                </View>
                <Spinner cancelable={true} visible={this.state.progressVisible} textContent={"加载中..."}
                         textStyle={{color: '#FFF'}}/>
            </View>
        );
    }

    followUs() {
        Alert.alert(
            '提示',
            '您可在微信-通讯录-添加朋友-查找公众号中搜索微微贷,点击关注,可更方便的获取我们的信息',
            [
                // {text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},
                {
                    text: '取消', onPress: () => {
                }, style: 'cancel'
                },
                {
                    text: '去关注', onPress: () => {
                    this.openWx()
                }
                },
            ],
            {cancelable: false}
        )
    }

    openMarket(){
            Linking.canOpenURL('market://details?id=vvdai').then(supported => { // weixin://  alipay://
                if (supported) {
                    Linking.openURL('market://details?id=vvdai');
                } else {
                    alert(`未找到应用市场`);
                }
            });
    }

    /**
     * 打开微信
     */
    openWx() {
        Linking.canOpenURL('weixin://').then(supported => { // weixin://  alipay://
            if (supported) {
                Linking.openURL('weixin://');
            } else {
                (`请先安装XXX`);
            }
        });
    }

    /**
     * 退出登录
     */
    logout() {
        this.setState({progressVisible: true});
        NetUtils.post(Api.logout, null, result => {
            if (result.code === 1) {
                Toast.message('登出成功');
                Api.isLogin = false;
                Api.userInfo = null;
                this.props.dispatch(logout());
                storage.remove({
                    key: 'userInfo'
                });
            } else {
                Toast.message(result.message);
            }
        }, () => this.setState({progressVisible: false}))
    }

}
const mapStateToProps = store => ({
    isLogin: store.login.isLogin,
});
export default connect(mapStateToProps)(MorePage);

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
    itemStyle: {
        backgroundColor: 'white',
        borderRadius: 4,
        borderWidth: 0.5,
        borderColor: '#aaaaaa',
        paddingLeft: 15,
        paddingRight: 15,
        marginTop: 8,
    }
});
